
var watchDog = {
    init : function() {
        //初始化按钮
        setMenuButtonOpermission();
        
		watchDog.loadData();
		
		$("#mainTable").height(getPageSize().WindowHeight);
    },
	
	loadData : function() {
		var params = {
					"msgId":"getWatchItems",
					"to":"watchDog",	
					"msg":{
						}
					};
		
		$.post("/watchDog/handleManage.req",JSON.stringify(params),function(result){
			var data = $.parseJSON(result);   
			
			if(DealErrorInfo(data,'没有查询到监控程序信息：')){	 
				// 加载表单数据wa
    			watchDog.showWatchDogList(data.msg);
			}															   
		});
        
    },
	
	watchItems:[],
	showWatchDogList:function(data){
		$("#windowList").html("");
		
		if(typeof(data.watchItems) == "undefined" )
		{
			data.watchItems = [];
		}
		
		var rl = data.watchItems;
		var i = 0;
		for(i=0;i < rl.length;i++)
		{
			watchDog.appendWindowHtml(rl[i]);
		}
		
		watchDog.watchItems = data.watchItems;
	},
	
	appendWindowHtml:function(watchItem)
	{
		var runningTime = "";
		if(watchItem.runningTime >= 3600)
		{
			runningTime = Math.floor((watchItem.runningTime)/3600) + "小时" + Math.floor((watchItem.runningTime%3600)/60) + "分钟";
		}
		else if(watchItem.runningTime >= 60)
		{
			runningTime = Math.floor((watchItem.runningTime)/60) + "分钟" + (watchItem.runningTime%60) + "秒";
		}
		else{
			runningTime =watchItem.runningTime + "秒";
		}
		
		var div = "<tr id='window"+watchItem.id+"'>"
				 +  "<td valign='middle' align='center' class='contentTd'>" + watchItem.id + "</td>"
				 +  "<td class='contentTd'>" + watchItem.programPath + " </td>"
				 +  "<td class='contentTd'>" + watchItem.rebootTime + " </td>"
				 +  "<td class='contentTd'>" + watchItem.rebootInterval + "</td>"
				 +  "<td class='contentTd'>" + runningTime + "</td>"
				 +  "<td class='contentTd'>" + watchItem.error + "</td>"
				 + 	"<td height='30px'  class='contentTd'>"
				 + 		"<input type='button'  class='windowBtn' value='修改' onclick='watchDog.editWatchItem("+watchItem.id+")' />"
				 + 		"<input type='button' class='windowBtn' value='删除' onclick='watchDog.delWatchItem("+watchItem.id+")' />"
				
				+ "</td></tr>"

		 $("#windowList").append(div);
	},
	
	
	addWatchDog:function()
	{
		var params = {
					"msgId":"addWatchItem",
					"to":"watchDog",	
					"msg":{
						"programPath":"新增",
						"rebootTime":"--:--:--",
						"rebootInterval":0,
						"enable":1
						}
					};
		
		$.post("/watchDog/handleManage.req",JSON.stringify(params),function(result){
			var data = $.parseJSON(result);   
			
			if(DealErrorInfo(data,'新增监控项失败：')){	 
				// 加载表单数据wa
    			watchDog.loadData();
			}															   
		});
	},
	
	delWatchItem:function(id)
	{
		var params = {
					"msgId":"delWatchItem",
					"to":"watchDog",	
					"msg":{
						"id":id
						}
					};
		
		$.post("/watchDog/handleManage.req",JSON.stringify(params),function(result){
			var data = $.parseJSON(result);   
			
			if(DealErrorInfo(data,'删除监控项失败：'))
			{	 
				// 加载表单数据wa
    			watchDog.loadData();
			}															   
		});
	},
	
	//编辑供应商信息
	editWatchItem:function(id)
	{
		var watchItem = null;
		var watchItems = watchDog.watchItems;
		for(var i=0;i<watchItems.length;i++)
		{
			if(id == watchItems[i].id)
			{
				watchItem = watchItems[i];
				break;
			}
		}
		
		if(watchItem == null)
		{
			return ;
		}

		$("#watchDogId").val(watchItem.id);
		$("#programPath").val(watchItem.programPath);
		$("#rebootTime").val(watchItem.rebootTime);
		$("#rebootInterval").val(watchItem.rebootInterval);
		
		$('#watchDogDiv').window({
			title:"修改监控项信息",
			left:300,
			top:20,
			width:500,
			height:300,
			modal:true
		});

		$('#watchDogDiv').window('open');
	},
	
	
	submitEditWatchDog:function()
	{		
		var watchDogId = parseInt($("#watchDogId").val());
		var programPath = $.trim($("#programPath").val());
		var rebootTime = $.trim($("#rebootTime").val());
		var rebootInterval = parseInt($.trim($("#rebootInterval").val()));
		
		var reg = new RegExp("\\\\","g");//g,表示全部替换。
		programPath = programPath.replace(reg,"/");
		console.log(programPath);

		var params = {
					"msgId":"setWatchItem",
					"to":"watchDog",	
					"msg":{
						"id":watchDogId,
						"programPath":programPath,
						"rebootTime":rebootTime,
						"rebootInterval":rebootInterval,
						"enable":1
						}
					};
		
		$.post("/watchDog/handleManage.req",JSON.stringify(params),function(result){
			var data = $.parseJSON(result);   
		
			if(DealErrorInfo(data,'修改监控项失败：'))
			{	 
				watchDog.loadData();
				$('#watchDogDiv').window('close');
			}															   
		});
	},
	
	cancelEditWatchDog:function()
	{
		$('#watchDogDiv').window('close');
	},
	
	showTips:function(msg)
	{
		$.messager.show({
				title:'提示',
				msg:msg,
				timeout:3000,
				showType:'slide'
			});
	},
};


